package com.example.soa2.controller;

import com.example.soa2.common.CommonResponse;
import com.example.soa2.entity.Material;
import com.example.soa2.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Controller
@RequestMapping("/material")
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    @GetMapping("/id/{id}")
    @ResponseBody
    public CommonResponse<Material> getMaterialById(@PathVariable("id")int id){
        return materialService.getMaterial(id);
    }

    @GetMapping("/name/{name}")
    @ResponseBody
    public CommonResponse<Material> getMaterialByName(@PathVariable("name")String name){
        return materialService.getMaterial(name);
    }

    @GetMapping("/materials")
    @ResponseBody
    public CommonResponse<List<Material>> getMaterials(){
        return materialService.getMaterials();
    }

    @PostMapping("/")
    @ResponseBody
    public CommonResponse<Material> addMaterial(@RequestBody Material material){
        return materialService.addMaterial(material);
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public CommonResponse<Material> deleteMaterial(@PathVariable("id") int id){
        return materialService.deleteMaterial(id);
    }

    @PutMapping("/{id}")
    @ResponseBody
    public CommonResponse<String> updateMaterial(@PathVariable("id") int id, @RequestBody Material material){
        return materialService.updateMaterial(id, material);
    }
 }
